import { wxLogin,wxAuthorization } from '@/request/api/wx';
import router from '@/router'

const state = {
  token: '',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    console.log(state.token)
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
        let query = router.currentRoute.query;
        wxAuthorization(query.pid).then((res)=>{
            let code = res.data.code;
            wxLogin(query.pid,code).then((res)=>{
                commit('SET_TOKEN',res.data.token)
                localStorage.setItem('token',res.data.token)
                localStorage.setItem('authName',res.data.token)
                localStorage.setItem('authUrl',res.data.token)
                location.reload()
                resolve()
            })
        })
    })
  },
}

export default {
  state,
  mutations,
  actions
}